package com.uetoken.cn.activity;

import android.content.Intent;
import android.os.Vibrator;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.FlashlightUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.uetoken.cn.R;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.utils.PhotoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/**
 * 二维码扫描
 */
public class ScanQrCodeActivity extends BaseActivity implements QRCodeView.Delegate {


    @BindView(R.id.zxingview)
    ZXingView mZXingView;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_activity_scan_qr_code_galley)
    TextView mTvGalley;
    @BindView(R.id.iv_flash_light)
    ImageView mIvFlashLight;

    private PhotoUtils photoUtils;
    private boolean isFlashlightEnable = false;


    @Override
    public int getLayoutId() {
        return R.layout.activity_scan_qr_code;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

        //状态栏沉浸式
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, android.R.color.white), true);
        BarUtils.setStatusBarLightMode(this, true);

        mZXingView.setDelegate(this);

        mTvTitle.setText(getResources().getString(R.string.str_scan_qr_code));
    }

    @Override
    public void initData() {
        photoUtils = new PhotoUtils();

        photoUtils.setPhotoPathListener(new PhotoUtils.PhotoPathListener() {
            @Override
            public void getPathSuccess(String path) {
                mZXingView.decodeQRCode(path);
            }
        });
    }

    @Override
    public void init() {

    }

    @OnClick({R.id.iv_back, R.id.tv_activity_scan_qr_code_galley, R.id.iv_flash_light})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_activity_scan_qr_code_galley:
                photoUtils.albumSelect(this);
                break;
            case R.id.iv_flash_light:
                if (isFlashlightEnable) {
                    isFlashlightEnable = false;
                    mZXingView.closeFlashlight();
                    mIvFlashLight.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.flash_light_close));
                } else {
                    isFlashlightEnable = true;
                    mZXingView.openFlashlight();
                    mIvFlashLight.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.flash_light_open));
                }
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 打开后置摄像头开始预览，但是并未开始识别
        mZXingView.startCamera();

        mZXingView.startSpotAndShowRect(); // 显示扫描框，并且延迟0.5秒后开始识别
    }

    @Override
    protected void onStop() {
        // 关闭摄像头预览，并且隐藏扫描框
        mZXingView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mZXingView.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();

        Intent intent = new Intent();
        intent.putExtra("qrCode", result);
        setResult(RESULT_OK, intent);
        finish();

    }

    /**
     * 手机震动
     */
    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        LogUtils.e("打开相机出错");
    }

}
